#include <iostream>
#include <set>
using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};

class Solution1
{
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
	{
		ListNode* cur1 = pHead1, * cur2 = pHead2;
		while (cur1 != cur2)
		{
			cur1 = cur1 != NULL ? cur1->next : pHead2;
			cur2 = cur2 != NULL ? cur2->next : pHead1;
		}
		return cur1;
	}
};

class Solution2
{
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
	{
		int count1 = 0;
		ListNode* cur1 = pHead1;
		while (cur1)
		{
			cur1 = cur1->next;
			count1++;
		}

		int count2 = 0;
		ListNode* cur2 = pHead2;
		while (cur2)
		{
			cur2 = cur2->next;
			count2++;
		}

		cur1 = pHead1, cur2 = pHead2;
		if (count1 > count2)
		{
			while (count1 != count2)
			{
				cur1 = cur1->next;
				count1--;
			}
		}
		else if (count1 < count2)
		{
			while (count1 != count2)
			{
				cur2 = cur2->next;
				count2--;
			}
		}

		while (cur1 && cur2 && cur1 != cur2)
		{
			cur1 = cur1->next;
			cur2 = cur2->next;
		}

		return cur1;
	}
};



int main()
{

	return 0;
}